package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.SysWxMenu;
import com.hzhc.mapper.SysWxMenuMapper;
import com.hzhc.service.SysWxMenuService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.SysWxMenuVo;
import com.hzhc.vo.TreeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 微信小程序菜单 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2023-11-07
 */
@Service
@RequiredArgsConstructor
public class SysWxMenuServiceImpl extends ServiceImpl<SysWxMenuMapper, SysWxMenu> implements SysWxMenuService {

    @Override
    public List<SysWxMenuVo> getMenuList(SysWxMenuVo vo) {
        List<SysWxMenuVo> list = baseMapper.selectMenuList(vo);
        return list;
    }

    @Override
    public ResultUtil<T> saveMenu(SysWxMenu vo) {
        vo.setCreateTime(LocalDateTime.now());

        boolean b = save(vo);
        //查找semtemId
        if(vo.getPid()==0){
            vo.setSystemMenuId(vo.getId().intValue());
            updateById(vo);
        }else{
            boolean flag=true;
            Long pid=vo.getPid();
            int i=0;
            while (flag){
                SysWxMenu sysWxMenu = baseMapper.selectById(pid);
                if(sysWxMenu.getPid()==0){
                    flag=false;
                    vo.setSystemMenuId(sysWxMenu.getId().intValue());
                    updateById(vo);
                }else{
                    flag=true;
                    pid=sysWxMenu.getPid();
                }
                i++;
                if(i>10){
                    break;
                }
            }
        }

        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> updateMenu(SysWxMenu vo) {
        boolean b = updateById(vo);
        //查找semtemId
        if(vo.getPid()!=0){
            boolean flag=true;
            Long pid=vo.getPid();
            int i=0;
            while (flag){
                SysWxMenu sysWxMenu = baseMapper.selectById(pid);
                if(sysWxMenu.getPid()==0){
                    flag=false;
                    vo.setSystemMenuId(sysWxMenu.getId().intValue());
                    updateById(vo);
                }else{
                    flag=true;
                    pid=sysWxMenu.getPid();
                }
                i++;
                if(i>10){
                    break;
                }
            }
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> deleteMenu(SysWxMenu vo) {
        boolean b = update(Wrappers.<SysWxMenu>lambdaUpdate()
                .eq(SysWxMenu::getId,vo.getId())
                .set(SysWxMenu::getDisable,1));
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public List<TreeVo> getMenuTree(Long pid) {
        List<TreeVo> retList = new ArrayList<TreeVo>();
        List<SysWxMenu> list = list(Wrappers.<SysWxMenu>lambdaQuery()
                .eq(SysWxMenu::getDisable,0));

        Map<Long, List<SysWxMenu>> menuMap = list.stream().collect(Collectors.groupingBy(SysWxMenu::getPid));
        // 是否需要展示最高节点
        List<SysWxMenu> glist = menuMap.get(pid);
        if (glist != null) {
            getMenuTreeList(retList, menuMap, glist);
        }
        return retList;
    }

    private void getMenuTreeList(List<TreeVo> retList, Map<Long, List<SysWxMenu>> menuMap, List<SysWxMenu> glist) {
        for (SysWxMenu menu : glist) {
            TreeVo treeVo = new TreeVo();
            treeVo.setId(menu.getId().intValue());
            treeVo.setLabel(menu.getTitle());
            treeVo.setChildren(getTreeChildren(menuMap, menu.getId()));
            retList.add(treeVo);
        }
    }

    private List<TreeVo> getTreeChildren(Map<Long, List<SysWxMenu>> menuMap, Long pid) {
        List<TreeVo> retList = new ArrayList<TreeVo>();
        List<SysWxMenu> childlist = menuMap.get(pid);
        if (childlist != null && !childlist.isEmpty()) {
            getMenuTreeList(retList, menuMap, childlist);
            return retList;
        }
        return null;
    }

}
